import { Controller, Get, Post, Put, Delete, Param, Body, UseGuards } from '@nestjs/common';
import { RoleService } from './role.service';
import { Role } from './role.entity';
import { CreateRoleDto } from './dto/create-role.dto';
import { UpdateRoleDto } from './dto/update-role.dto';
import { JwtHttpGuard } from '../auth/jwt-http.guard';

@Controller('role')
@UseGuards(JwtHttpGuard)
export class RoleController {
  constructor(private readonly roleService: RoleService) {}

  @Get('list')
  async findAll(): Promise<Role[]> {
    return await this.roleService.findAll();
  }

  @Get(':id')
  async findOne(@Param('id') id: string): Promise<Role | null> {
    return await this.roleService.findOne(+id);
  }

  @Post()
  async create(@Body() createRoleDto: CreateRoleDto): Promise<Role> {
    return await this.roleService.create(createRoleDto);
  }

  @Put(':id')
  async update(@Param('id') id: string, @Body() updateRoleDto: UpdateRoleDto): Promise<Role | null> {
    return await this.roleService.update(+id, updateRoleDto);
  }

  @Delete(':id')
  async remove(@Param('id') id: string): Promise<void> {
    return await this.roleService.remove(+id);
  }

  @Get(':id/policies')
  async getRolePolicies(@Param('id') id: string): Promise<any[]> {
    const role = await this.roleService.findOne(+id);
    if (!role) {
      return [];
    }
    return await this.roleService.getRolePolicies(role.name);
  }

  @Post(':id/policies')
  async assignPermission(
    @Param('id') id: string,
    @Body('resource') resource: string,
    @Body('action') action: string
  ): Promise<boolean> {
    const role = await this.roleService.findOne(+id);
    if (!role) {
      return false;
    }
    return await this.roleService.assignPermissionToRole(role.name, resource, action);
  }
  
  // 新增接口：为角色删除权限
  @Post(':id/policies/remove')
  async removePermission(
    @Param('id') id: string,
    @Body('resource') resource: string,
    @Body('action') action: string
  ): Promise<boolean> {
    const role = await this.roleService.findOne(+id);
    if (!role) {
      return false;
    }
    return await this.roleService.removePermissionFromRole(role.name, resource, action);
  }
  
  // 新增接口：删除角色的所有权限
  @Post(':id/policies/clear')
  async clearPermissions(@Param('id') id: string): Promise<boolean> {
    const role = await this.roleService.findOne(+id);
    if (!role) {
      return false;
    }
    return await this.roleService.removeAllPermissionsFromRole(role.name);
  }
  
  // 获取角色的分类权限策略
  @Get(':id/category-policies')
  async getRoleCategoryPolicies(@Param('id') id: string): Promise<any[]> {
    const role = await this.roleService.findOne(+id);
    if (!role) {
      return [];
    }
    return await this.roleService.getRoleCategoryPolicies(role.name);
  }
  
  // 为角色分配分类权限
  @Post(':id/category-policies')
  async assignCategoryPermission(
    @Param('id') id: string,
    @Body('categoryId') categoryId: number,
    @Body('action') action: string
  ): Promise<boolean> {
    const role = await this.roleService.findOne(+id);
    if (!role) {
      return false;
    }
    return await this.roleService.assignCategoryPermissionToRole(role.name, categoryId, action);
  }
  
  // 删除角色的分类权限
  @Post(':id/category-policies/remove')
  async removeCategoryPermission(
    @Param('id') id: string,
    @Body('categoryId') categoryId: number,
    @Body('action') action: string
  ): Promise<boolean> {
    const role = await this.roleService.findOne(+id);
    if (!role) {
      return false;
    }
    return await this.roleService.removeCategoryPermissionFromRole(role.name, categoryId, action);
  }
  
  // 删除角色的所有分类权限
  @Post(':id/category-policies/clear')
  async clearCategoryPermissions(@Param('id') id: string): Promise<boolean> {
    const role = await this.roleService.findOne(+id);
    if (!role) {
      return false;
    }
    return await this.roleService.removeAllCategoryPermissionsFromRole(role.name);
  }
}